package com.lzzy.web.Controller;

import com.lzzy.dao.CommentRepository;
import com.lzzy.model.domain.Article;
import com.lzzy.service.impl.ArticleServicelmpl;
import com.lzzy.service.impl.CommentServicelmpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class BackController {

    @Autowired
    private ArticleServicelmpl articleServicelmpl;
    @Autowired
    private CommentServicelmpl commentServicelmpl;
    @Autowired
    private CommentRepository commentRepository;

    @GetMapping("/my")
    public String my(Model model) {
        return "back/My";
    }


    //跳转文章新增页
    @GetMapping("/manage/blog/and")
    public String publish(Model model) {
        return "back/article_publish";
    }

    //文章新增发布
    @PostMapping("/back/publish")
    public String addArticle(@RequestParam("title") String title, @RequestParam("category") String category, @RequestParam("content") String content){
        Article article =  articleServicelmpl.addArticle(title, category, content);
        return "redirect:/index";
    }

    //跳转文章删除页
    @GetMapping("/manage/blog")
    public String delet(Model model){
        model.addAttribute("testScope",articleServicelmpl.findarticleall());
        return "back/article_list";
    }

    //删除文章
    @GetMapping("/article/delete/{id}")
    public String deleteArticle(@PathVariable("id") Integer id){
        articleServicelmpl.deleteById(id);
        commentServicelmpl.deleteComments(id);

        return "redirect:/manage/blog";
    }

    //跳转文章修改页
    @GetMapping("/article/modify/{id}")
    public String Article(@PathVariable("id") Integer id,Model model){
      Article article =  articleServicelmpl.findArticleById(id);
      model.addAttribute("articleAll",article);
        return "back/article_modify ";
    }

    //文章修改
    @PostMapping("/back/article/modify/{id}")
    public String addArticle(@PathVariable("id") Integer id,@RequestParam("title") String title, @RequestParam("category") String category, @RequestParam("content") String content){
       articleServicelmpl.updateArticle(title,content,category,id);
        return "redirect:/article/modify/"+id;
    }

    //跳转评论删除页
    @GetMapping("/manage/comment")
    public String deletecomment(Model model){
        model.addAttribute("c",commentRepository.findAll());
        return "/back/comment_list";
    }

    //id删除
    @GetMapping("/comment/delete/{id}")
    public String deletecomment(@PathVariable("id") Integer id) {
        commentServicelmpl.deleteDiscuss(id);
        return "redirect:/manage/comment";
    }


}
